<?php
namespace Admin\Controller;
use Think\Controller;
class SystemController extends Controller {

	//
	//	角色相关  
	//
	
	//角色管理页
    public function role($roleNameFilter=''){
		
		//查看角色权限为1
		$auth		= 1;
		tag('auth_check',$auth);
		
		$pageRows	= 10; //每页显示数
		
		$Role		= D('Role','Logic');
		if($roleNameFilter){
			$condition['rname'] = array('like','%'.$roleNameFilter.'%');
			$this->assign('roleNameFilter',$roleNameFilter);
		}
		$show		= $Role->getPage($pageRows,$condition);	
		$list		= $Role->getList($pageRows,I('get.p/d',1),$condition);
		$list		= $Role->getOADetail($list);

		$this->assign('list',$list);
		$this->assign('page',$show);
		$this->display();
	}
	
	
	//添加角色处理
	public function role_add_do(){

		//添加角色权限为2
		$auth		= 2;
		tag('auth_check',$auth);
	
		if(I('get.oper_auth')==''){
			$this->error('权限最少要选择一个！');
		}		
		$Role				= D('Role','Logic');
		if (!$Role->create(I('get.'))){
			$this->error($Role->getError());
		}
		$Role->oper_auth	= implode(',',$Role->oper_auth);
		$result = $Role->add();
		if($result){
			$this->success('添加成功', 'role');
		} else {
			$this->error('添加失败');
		}		
	}

	//ajax获取角色数据
	public function role_get_data($id){

		//查看角色权限为1
		$auth		= 1;
		tag('auth_check',$auth);
	
		$Role		= D('Role','Logic');
		$data		= $Role->find($id);
		$this->ajaxReturn($data,'JSON');
	}
	
	//ajax修改角色状态
	public function role_update_status(){

		//修改角色权限为3
		$auth		= 3;
		tag('auth_check',$auth);
	
		C('TOKEN_ON',false);
		$Role				= D('Role','Logic');
		if (!$Role->create(I('get.'))){
			$this->ajaxReturn(0);
		}
		$result = $Role->save();
		$this->ajaxReturn($result);	
	}
	
	//修改角色处理
	public function role_update_do(){
		
		//修改角色权限为3
		$auth		= 3;
		tag('auth_check',$auth);		
		
		if(I('get.oper_auth')==''){
			$this->error('权限最少要选择一个！');
		}			
		$Role				= D('Role','Logic');
		if (!$Role->create(I('get.'))){
			$this->error($Role->getError());
		}
		$Role->oper_auth	= implode(',',$Role->oper_auth);
		$result = $Role->save();
		
		if($result){
			$this->success('修改成功', 'role');
		} else {
			$this->error('修改失败');
		}		
	}	
	
	//删除角色处理
	public function deleteRole($id){

		//关闭表单令牌验证
		C('TOKEN_ON',false);
		
		//删除角色权限为4
		$auth		= 4;
		tag('auth_check',$auth);
	
		$Role				= D('Role','Logic');
		if (!$Role->create(I('get.'))){
			$this->error($Role->getError());
		}
		$result = $Role->delete($id);
		
		if($result){
			//修改管理员所属该id的角色为0
			$User				= D('User','Logic');
			$User->create();
			$data['role_id']	= 0;
			$User->where('role_id='.$id)->data($data)->save();			
			
			$this->success('删除成功', 'role');
		} else {
			$this->error('删除失败');
		}		
	}	


	//
	//	管理员相关  
	//
	
	//管理员管理
	public function user($adminFilter=''){

		//查看管理员权限为5
		$auth		= 5;
		tag('auth_check',$auth);
	
		$pageRows	= 10; //每页显示数
		$User		= D('User','Logic');
		if($adminFilter){
			$condition['username'] = array('like','%'.$adminFilter.'%');
			$this->assign('adminFilter',$adminFilter);
		}			
		$show		= $User->getPage($pageRows,$condition);	
		$list		= $User->getList($pageRows,I('get.p/d',1),$condition);
		$list		= $User->getRoleDetail($list);		
		
		$this->assign('list',$list);
		$this->assign('page',$show);		
		$this->display();
	}	

	//添加管理员帐号处理
	public function user_add_do(){

		//添加管理员权限为6
		$auth		= 6;
		tag('auth_check',$auth);
	
		if(I('get.role_id')==''){
			$this->error('必须要选择一个角色！');
		}		
		$User				= D('User','Logic');
		if (!$User->create(I('get.'))){
			$this->error($User->getError());
		}
		$User->create_time	= time();
		$result = $User->add();
		if($result){
			$this->success('添加成功', 'user');
		} else {
			$this->error('添加失败');
		}		
	}
	
	//ajax修改帐号状态
	public function user_update_status(){
		
		//修改管理员权限为7
		$auth		= 7;
		tag('auth_check',$auth);		
		
		C('TOKEN_ON',false);
		$User				= D('User','Logic');
		if (!$User->create(I('get.'))){
			$this->ajaxReturn(0);
		}
		$result = $User->where('id='.I('get.id'))->setField('is_active',I('get.is_active'));
		$this->ajaxReturn($result);	
	}	
	
	//ajax获取角色数据
	public function user_get_data($id){
		
		//查看管理员权限为5
		$auth		= 5;
		tag('auth_check',$auth);		
		
		$User		= D('User','Logic');
		$data		= $User->find($id);
		$this->ajaxReturn($data,'JSON');
	}

	//修改帐号处理
	public function user_update_do(){

		//修改管理员权限为7
		$auth		= 7;
		tag('auth_check',$auth);
		
		if(intval(I('get.role_id'))==0){
			$this->error('权限最少要选择一个！');
		}			
		$User				= D('User','Logic');
		if (!$User->create(I('get.'))){
			$this->error($User->getError());
		}
		$result = $User->save();
		
		if($result){
			$this->success('修改成功', 'user');
		} else {
			$this->error('修改失败');
		}		
	}	
	
	//删除角色处理
	public function deleteUser($id){

		//关闭表单令牌验证
		C('TOKEN_ON',false);	
	
		//删除管理员权限为8
		$auth		= 8;
		tag('auth_check',$auth);
	
		$User				= D('User','Logic');
		if (!$User->create(I('get.'))){
			$this->error($User->getError());
		}
		$result = $User->delete($id);
		
		if($result){
			$this->success('删除成功', 'User');
		} else {
			$this->error('删除失败');
		}		
	}	
	
}